/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cryptography;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import platform.TenorCryptoUtils;
import zorg.platform.Digest;

/**
 *
 * @author Tomasz Nowak
 */
public class HashWrapper implements Digest
{
	MessageDigest digest;

	public HashWrapper( String algorithm ) throws NoSuchAlgorithmException
	{
		digest = MessageDigest.getInstance(algorithm);
	}

	@Override
	public int getDigestLength()
	{
		return digest.getDigestLength();
	}

	@Override
	public void update( byte[] buffer )
	{
		digest.update(buffer);
	}

	@Override
	public void update( byte[] buffer, int offset, int length )
	{
		digest.update(TenorCryptoUtils.subArray(buffer, offset, length));
	}

	@Override
	public byte[] getDigest()
	{
		return digest.digest();
	}

	@Override
	public int getDigest( byte[] buffer, int offset, boolean reset )
	{
		byte[] result = digest.digest();
		for ( int i = 0; i < result.length; ++i )
		{
			buffer[i + offset] = result[i];
		}
		if ( reset )
		{
			digest.reset();
		}
		return 0;
	}
}
